﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using APMS.WebSite.Common;
using APMS.WebSite.Models.DataStore;
using APMS.WebSite.Models.ViewData.Pages;

namespace APMS.WebSite.Controllers
{
    public class Todo_itemController : BaseController
    {
        public ActionResult Index()
        {
            // Add action logic here
            throw new NotImplementedException();
        }


        //"todo",      // Route name


        public ActionResult create(int projectId, int todoId, String todo_item_detail
            , String assign_to, DateTime death_line)
        {
            //throw new NotImplementedException();   
            String username = "admin";
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            ToDoItem newToDoItem = new ToDoItem();
            newToDoItem.ToDoID = todoId;
            newToDoItem.ToDoItemDetail = todo_item_detail;
            newToDoItem.DeathLine = death_line;
            newToDoItem.AssignTo = assign_to; ;
            newToDoItem.AssignTo = username;
            newToDoItem.ModifyDate = DateTime.Now;
            newToDoItem.AddDate = DateTime.Now;


            db.ToDoItem.Add(newToDoItem);
            db.SubmitChanges();
            return View("test");
        }

        public ActionResult Delete(int projectId, int todoId, int id)
        {
            //throw new NotImplementedException();
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();


            try
            {
                ToDoItem deleteToDoItem = db.ToDoItem.Single(p => p.ToDoItemID == id);
                db.ToDoItem.Remove(deleteToDoItem);
                db.SubmitChanges();
            }
            catch (InvalidOperationException ex)
            {
                System.Console.WriteLine("called on set with zero items");
            }
            ////////////////////// DELETE DATA

            return Index();

            return View("test");
        }



        public ActionResult Update(int projectId, int id)
        {
            // Initialize data required by master page

            ActionResult exception = base.InitializeAction();
            if (exception != null)
                return exception;

            // Ser Window Title
            base.SetTitle("ToDo Item | " + id);

            // SELECT * FROM user WHERE user.UserName = '{currentUser}'
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            var query =
                from todoitem in db.ToDoItem
                where
                    todoitem.ToDoItemID == id
                select todoitem;

            // Prepare ViewData
            ManageTodo_itemViewData viewData = new ManageTodo_itemViewData();
            viewData.CurrentTodo_item = query.First<ToDoItem>();

            //TODO: Handle both POST and GET methods
            if (Request.HttpMethod == Constants.HttpMethod.POST)
            {
                // If POST means UPDATE the value from Request.Form to DB
                // If GET means VIEW the value from DB
                #region Update record back to DB
                DateTime DeathLine = DateTime.Parse(Request.Form["DeathLine"]);
                viewData.CurrentTodo_item.AssignTo = Request.Form["AssignTo"];
                viewData.CurrentTodo_item.DeathLine = DeathLine;
                viewData.CurrentTodo_item.ToDoItemDetail = Request.Form["ToDoItemDetail"];
                viewData.CurrentTodo_item.ModifyDate = DateTime.Now;

                viewData.IsCurrentTodo_itemUpdate = true;

                db.SubmitChanges();
                #endregion
            }
            else
            {
                viewData.IsCurrentTodo_itemUpdate = false;
            }

            return View("Update", viewData);
        }

        public ActionResult List(int projectId, int todoId)
        {
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            var query =
                from todoitem in db.ToDoItem
                where
                    todoitem.ToDoID == todoId
                select todoitem;

            ToDo todo = db.ToDo.Single(p => p.ToDoID == todoId);

            ViewData["todo"] = todo;
            ViewData["all"] = query.ToList();

            return View("test");
        }


    }
}
